# -*- coding: utf8 -*-
import pygtk
pygtk.require('2.0')
import gtk
import urllib2, cookielib, urllib, time, math

class YeahBot:
  def __init__(self):
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.connect("destroy", self.destroy)
    
    vbox = gtk.VBox()
    self.window.add(vbox)
    vbox.show()

    self.entry = gtk.Entry()
    self.entry.connect("key_release_event", self.entry_keyin)
    vbox.add(self.entry)
    #vbox.pack_start(self.entry)
    self.entry.show()  

    self.image = gtk.Image()
    vbox.add(self.image)
    #vbox.pack_start(self.image)
    self.image.show()
    
    self.window.show()
    

    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    opener.addheaders = [('User-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 1.1.4322; CIBA)')]
    self.opener = opener
    self.username = ""

  def entry_keyin(self, widget, event):
    if event.keyval == 65293:
      self.entry.set_editable(False)
      if self.username:
        self.reg2(verifycode)
      else:
	self.reg()
    return False

  def destroy(self, widget, data=None):
    gtk.main_quit()
  
  def checkusername(self):
    self.opener.open("http://reg.mail.yeah.net/reg/reg1.jsp")
    time.sleep(2)
    req = urllib2.Request('http://reg.mail.yeah.net/reg/checkusername.jsp?submit=1', urllib.urlencode({'username':self.username, 'submit': 'next'})+"\r\n", {'Referer': 'http://reg.mail.yeah.net/reg/reg1.jsp'})
    res = self.opener.open(req)
    if res.read().find("gotoNextStep") != -1:
      return True
    else:
      return False

  def getverifycode(self):
    urllib.urlretrieve('http://reg.mail.yeah.net/reg/magic126v2.jsp', 'verifycode.jpg')
    self.image.set_from_file("verifycode.jpg");
    self.entry.set_editable(True)

  def reg2(self, verifycode):
    print "got verifycode ", verifycode
    req = urllib2.Request('http://reg.mail.yeah.net/reg/servlet/reg', urllib.urlencode({
        "password":	"123456",
        "password_cf":	"123456",
        "alt_email":"",	
        "mobile":	"13642342342",
"question":	"您最爱读的书是?".encode("gb2312"),
"custom_question": "",	
"answer":	"sdfsdf",
"year":	"1997",
"month":	"3",
"date":	"24",
"gender":	"0",
"verify_code":	verifycode.encode("gb2312"),
"readed": "",	
"submit":	"下一步".encode("gb2312")}), {'Referer': 'http://reg.mail.yeah.net/reg/reg2.jsp'})
    res = self.opener.open(req)
    if res.read().find("success") != -1:
      self.entry.set_editable(True)
      print "success"
    else:
      self.username = ""
      self.entry.set_text("")
      print "failed"


  def reg(self):
    self.username = "imcomming" + str(math.floor(time.time()))
    print "start reg " + self.username + "-----------"
    if self.checkusername():
      print "go on to get verifycode"
      self.getverifycode()
    else:
      self.username = ""
      self.entry.set_text("")
      print "failed"

      
  def main(self):
    gtk.main()

if __name__ == '__main__':
  bot = YeahBot()

  bot.main()